import datetime
import logging
import time
import urllib
from urllib import request, parse
import ssl
import json

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
f_handler = logging.FileHandler('trace3.log')
f_handler.setLevel(logging.INFO)
f_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(filename)s[:%(lineno)d] - %(message)s"))
logger.addHandler(f_handler)

context = ssl._create_unverified_context()

url = "https://maicai.api.ddxq.mobi/guide-service/productApi/productDetail/info"
uid = "59786872dc759e40ec5a5e68"
station_id = "56f35790916edf7940ad8588"
package_order = {
    "payment_order": {
        "reserved_time_start": 1649716200,
        "reserved_time_end": 1649730600,
        "price": "41.40",
        "freight_discount_money": "5.00",
        "freight_money": "5.00",
        "order_freight": "0.00",
        "parent_order_sign": "e2556017c525afd03ac17cfc5c7b3e9f",
        "product_type": 1,
        "address_id": "5eb951e9c7316f5e8b9e79c9",
        "form_id": "a75ca47eb2cf48fb85985b9103fc05a2",
        "receipt_without_sku": "null",
        "pay_type": 6,
        "vip_money": "",
        "vip_buy_user_ticket_id": "",
        "coupons_money": "",
        "coupons_id": ""
    },
    "packages": [
        {
            "products": [
                {
                    "id": "622f0cc59675ba7699cec4e6",
                    "parent_id": "",
                    "count": 3,
                    "cart_id": "622f0cc59675ba7699cec4e6",
                    "price": "13.8",
                    "product_type": 0,
                    "is_booking": 0,
                    "product_name": "陕西红富士4个700G",
                    "small_image": "https://imgnew.ddimg.mobi/product/0a38e01e37384ee191d2393f43b1347d.jpg?width=800&height=800",
                    "sale_batches": {
                        "batch_type": -1
                    },
                    "order_sort": 1,
                    "sizes": []
                }
            ],
            "total_money": "41.40",
            "total_origin_money": "41.40",
            "goods_real_money": "41.40",
            "total_count": 3,
            "cart_count": 3,
            "is_presale": 0,
            "instant_rebate_money": "0.00",
            "total_rebate_money": "0.00",
            "used_balance_money": "0.00",
            "can_used_balance_money": "0.00",
            "used_point_num": 0,
            "used_point_money": "0.00",
            "can_used_point_num": 0,
            "can_used_point_money": "0.00",
            "is_share_station": 0,
            "only_today_products": [],
            "only_tomorrow_products": [],
            "package_type": 1,
            "package_id": 1,
            "front_package_text": "即时配送",
            "front_package_type": 0,
            "front_package_stock_color": "#2FB157",
            "front_package_bg_color": "#fbfefc",
            "eta_trace_id": "",
            "reserved_time_start": 1649716200,
            "reserved_time_end": 1649730600,
            "soon_arrival": "",
            "first_selected_big_time": 1
        }
    ]
}

getDict = {
    "uid": uid,
    "longitude": "121.425446",
    "latitude": "31.237419",
    "station_id": station_id,
    "city_number": "0101",
    "api_version": "9.49.2",
    "app_version": "2.82.0",
    "applet_source": "",
    "channel": "applet",
    "app_client_id": "4",
    "sharer_uid": "",
    "s_id": "7d436ca457008b1c829efe8ac176a26b",
    "openid": "osP8I0WwHO78fDdWv6vaJWMVXZFg",
    "h5_source": "",
    "device_token": "WHJMrwNw1k/FKPjcOOgRd+J1ee6rW3n/PRrH6Kq8m9g66TlQ+UvE66AmDe51vPg9Mc/XtPI0QeUz7MbJchBByraRk0jv7nINFdCW1tldyDzmauSxIJm5Txg==1487582755342",
    "image": "https%3A%2F%2Fimg.ddimg.mobi%2Fproduct%2F69cf3a99013ed1567428031112.jpg%21deliver.product.list",
    "product_id": "5ec2724cb0055a0b5a3d7698",
    "product_name": "%E3%80%90%E4%BA%A7%E5%9C%B0%E7%89%B9%E8%89%B2%E3%80%91%E9%87%91%E5%9D%9B%E6%B0%B4%E8%8A%B9%20350g%2F%E4%BB%BD",
    "id": "5ec2724cb0055a0b5a3d7698",
    "pageid": "",
    "cid": "",
    "nars": "841f91e7e639aa9c95a9a239039b4745",
    "sesi": "oZgxalKbe0f7c81f2a0d378f3c5f1c06b6122ad"
}
headers = {
    "Host": "maicai.api.ddxq.mobi",
    "content-type": "application/x-www-form-urlencoded",
    # "Connection": "keep-alive",
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36 MicroMessenger/7.0.9.501 NetType/WIFI MiniProgramEnv/Windows WindowsWechat",
    "ddmc-api-version": "9.49.2",
    "ddmc-app-client-id": "4",
    "ddmc-build-version": "2.82.0",
    "ddmc-channel": "applet",
    "ddmc-city-number": "0101",
    "ddmc-device-id": "osP8I0WwHO78fDdWv6vaJWMVXZFg",
    "ddmc-ip": "",
    "ddmc-latitude": "31.237419",
    "ddmc-longitude": "121.425446",
    "ddmc-os-version": "[object Undefined]",
    "ddmc-station-id": station_id,
    "ddmc-uid": uid,
    "Referer": "https://servicewechat.com/wx1e113254eda17715/422/page-frame.html",
    # "Accept-Encoding": "gzip, deflate, br",
    "Cookie": "DDXQSESSID=10fa0251c71abbd2bd914069b61d3074"
}
from urllib.parse import urlencode
url_params = urlencode(getDict)
url = f"{url}?{url_params}"
print(f"requesting to {url}")
request = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(request, context=context)
htmlDom = response.read().decode('utf-8')
print(htmlDom)
